# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Milan P. Stanić <mps@arvanta.net>
pkgname=linux-tools
pkgver=6.6.18
case $pkgver in
	*.*.*)	_kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=1
pkgdesc="Linux kernel tools meta package"
url="https://www.kernel.org/"
arch="all"
license="GPL-2.0-only"
depends="perf cpupower $pkgname-gpio $pkgname-iio"
depends_dev="pciutils-dev readline-dev gettext-dev"
_llvmver=16
makedepends="
	$depends_dev
	asciidoc
	autoconf
	automake
	bash
	binutils-dev
	bison
	clang$_llvmver
	diffutils
	elfutils-dev
	findutils
	flex
	glib-dev
	libbpf-dev
	libcap-dev
	libnl3-dev
	libtool
	libtraceevent-dev
	eudev-dev
	libunwind-dev
	linux-headers
	llvm$_llvmver-dev
	ncurses-dev
	numactl-dev
	openssl-dev
	python3-dev
	slang-dev
	sysfsutils-dev
	xmlto
	zlib-dev
	zstd-dev
	"
subpackages="
	$pkgname-doc
	perf
	perf-bash-completion:perf_bashcomp:noarch
	cpupower
	cpupower-openrc:cpupower_openrc
	cpupower-bash-completion:cpupower_bashcomp:noarch
	$pkgname-dev
	$pkgname-gpio
	$pkgname-iio
	$pkgname-spi
	$pkgname-tmon
	$pkgname-usbip
	$pkgname-usbip-openrc:usbip_openrc
	bpftool
	bpftool-bash-completion:bpftool_bashcomp:noarch
	"
source="https://kernel.org/pub/linux/kernel/v6.x/linux-$_kernver.tar.xz
	cpupower-libs.patch
	dt_lnk.patch
	perf-doc.patch
	cpupower.initd
	cpupower.confd
	usbip.initd
	usbip.confd
	"
# append patch for non .0
case $pkgver in
*.*.0)
	;;
*.*.*)
	source="
		$source
		https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz
		"
	;;
esac

builddir="$srcdir"/linux-$_kernver

prepare() {
	cd "$srcdir"/linux-$_kernver
	if [ "${pkgver%.0}" = "$pkgver" ]; then
		msg "Applying patch-$pkgver.xz"
		unxz -c < "$srcdir"/patch-$pkgver.xz | patch -p1 -N
	fi

	# tools/perf/Makefile is just a wrapper around tools/perf/Makefile.perf
	# which calls the latter with a -j make(1) option set. However, it discards
	# variables set via the command-line in the process (e.g. mandir or prefix).
	# As such, it prevents us from correctly installing man pages with our current
	# setup. Just symlink the former to the latter to fix that.
	ln -fs Makefile.perf "$builddir"/tools/perf/Makefile

	default_prepare
}

_make_tools() {
	make -C "$builddir"/tools \
		VERSION=$pkgver-$pkgrel NLS=false LIBINTL_LIBS=-lintl WERROR=0 \
		prefix=/usr libdir=/usr/lib lib=lib mandir=/usr/share/man \
		"$@"
}

build() {
	_make_tools perf cpupower gpio iio spi tmon bpf/bpftool
	( cd tools/usb/usbip ;
	sed -i 's,-Wall -Werror -Wextra,,' configure.ac ;
	./autogen.sh
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--libdir=/usr/lib \
		--datarootdir=/usr/share ;
	make )
}

package() {
	mkdir -p "$pkgdir"
	_make_tools DESTDIR="$pkgdir" \
		perf_install cpupower_install gpio_install iio_install spi_install

	[ "$CARCH" = x86_64 ] && _make_tools DESTDIR="$pkgdir" \
		intel-speed-select_install turbostat_install x86_energy_perf_policy_install

	make -C "$builddir/tools/bpf/bpftool" prefix=/usr \
		DESTDIR="$pkgdir" install
	( cd tools/thermal/tmon;
		make install INSTALL_ROOT="$pkgdir"; )
	( cd tools/usb/usbip ;
		make DESTDIR="$pkgdir" install; )

	# we only support bash completions in usr/share/bash-completion
	mv "$pkgdir"/etc/bash_completion.d/* "$pkgdir"/usr/share/bash-completion/completions/
	rmdir "$pkgdir"/etc/bash_completion.d

	install -Dm755 "$srcdir"/cpupower.initd "$pkgdir"/etc/init.d/cpupower
	install -Dm644 "$srcdir"/cpupower.confd "$pkgdir"/etc/conf.d/cpupower
	install -Dm755 "$srcdir"/usbip.initd "$pkgdir"/etc/init.d/usbip
	install -Dm644 "$srcdir"/usbip.confd "$pkgdir"/etc/conf.d/usbip
}

cpupower() {
	pkgdesc="Linux kernel tool to set CPU power features"
	depends=""

	amove \
		usr/bin/cpu* \
		usr/sbin/cpu* \
		usr/lib/libcpu*.so.* \
		etc/cpu*

	if [ "$CARCH" = x86_64 ]; then
		amove usr/bin/intel-speed-select
		amove usr/bin/turbostat
		amove usr/bin/x86_energy_perf_policy
	fi
}

cpupower_openrc() {
	pkgdesc="Linux kernel tool to set CPU power features (OpenRC init scripts)"
	depends=""
	install_if="cpupower=$pkgver-r$pkgrel openrc"

	amove \
		etc/init.d/cpupower \
		etc/conf.d/cpupower
}

cpupower_bashcomp() {
	pkgdesc="Bash completion for cpupower"
	install_if="cpupower=$pkgver-r$pkgrel bash-completion"

	amove usr/share/bash-completion/completions/cpupower
}

usbip() {
	pkgdesc="utilities for USB device sharing over IP network"
	replaces="usbip-utils"
	provides="usbip-utils=$pkgver-r$pkgrel"
	depends=""

	amove \
		usr/sbin/usb* \
		usr/lib/libusb*
}

usbip_openrc() {
	pkgdesc="utilities for USB device sharing over IP network (OpenRC init scripts)"
	depends=""
	install_if="$pkgname-usbip=$pkgver-r$pkgrel openrc"

	amove \
		etc/init.d/usbip \
		etc/conf.d/usbip
}

perf() {
	pkgdesc="Linux kernel performance auditing tool"
	depends="libtraceevent-plugins"

	amove \
		usr/bin/perf \
		usr/bin/trace \
		usr/libexec/perf-core

	# /usr/share/perf-core is apprently not installed on all arches
	if [ -e "$pkgdir"/usr/share/perf-core ]; then
		amove usr/share/perf-core
	fi

	# just tests
	rm -r "$subpkgdir"/usr/libexec/perf-core/tests
}

perf_bashcomp() {
	replaces="$pkgname-bash-completion" # Backward compatibility
	pkgdesc="Bash completion for perf"
	install_if="perf=$pkgver-r$pkgrel bash-completion"

	amove usr/share/bash-completion/completions/perf
}

gpio() {
	pkgdesc="Linux kernel GPIO tools"
	depends=""

	amove usr/bin/*gpio*
}

iio() {
	pkgdesc="Linux kernel IIO tools"
	depends=""

	amove usr/bin/*iio*
}

spi() {
	pkgdesc="Linux kernel SPI tools"
	depends=""

	amove usr/bin/spi*
}

tmon() {
	pkgdesc="Linux kernel thermal monitor tools"
	replaces="tmon"
	depends=""

	amove usr/bin/tmon
}

bpftool() {
	pkgdesc="Linux kernel bpf manipulation tool"

	amove usr/sbin/bpftool
}

bpftool_bashcomp() {
	pkgdesc="Bash completion for bpftool"
	install_if="bpftool=$pkgver-r$pkgrel bash-completion"

	amove usr/share/bash-completion/completions/bpftool
}

doc() {
	default_doc
}

sha512sums="
458b2c34d46206f9b4ccbac54cc57aeca1eaecaf831bc441e59701bac6eadffc17f6ce24af6eadd0454964e843186539ac0d63295ad2cc32d112b60360c39a35  linux-6.6.tar.xz
a46e3a84b00a39a356618831d0ddfb7f0d10f0a3799d1307ba2cc832e73c01f8d637a4e801a6dd25025f6f13155c6ad8b836422ff72d365e51063ac0bf907f52  cpupower-libs.patch
0657b461659e79c1926379aab30e7b7ab2d59eaf3e686f1dce468b79eb1320c188c14a0507361660abd2553bccffa9e149fa5b6e4d770d10c99bf5dafd2e05ee  dt_lnk.patch
2092b96d64e6c379ae52700b3c1bf459581afea69622a296821381e74ecafa4a08cea24eab002ad2152878a4508918de7807ac98ba72faf2d4dea18cc275933f  perf-doc.patch
97ecfeaaaa8efca7af585316b843bf7be885e88095bbdfbf52bc9a96b14dc862435482781e205ea022ab208978aaa4aabb317354fab890ed514a5088ae5045c8  cpupower.initd
63cd12a4d8782198c71662e9205e346e064b65bae5e3e175b5bc43bdf7ec6ddf675b60349750afe3c37c8a925f9ae9a93530671821342581bd282a69e0773b8c  cpupower.confd
fcbd64d844c9bc187d08cef5995e91a46c0df78deb24e96ac9210c0e2c730eca0301970d9b8ffbf003df274682d05072431a26b59d8c491f396618268a12ec92  usbip.initd
eb8de617e27c4d5fdfee9c442e8f74b0afb4d0fe7b59eca3a19629eb70fea7e09b3c125bc968aa8810d845ce661c829bd0f3fdb2288664f2cccf423bc0ae6ae8  usbip.confd
2fe42e370b61789aed4b257b95c9c008fbca3f7980d5ce58be4fa7993b4a0748890a64c7d89c9211f48c7e48f6e40bac11621ebf29904617e1105a6f37068d2f  patch-6.6.18.xz
"
